package com.quanyan.pay.service.impl;

import com.quanyan.common.exception.BizException;
import com.quanyan.pay.entity.AccountPayRelation;
import com.quanyan.pay.entity.AccountPayRelationExample;
import com.quanyan.pay.exception.RespPayEnum;
import com.quanyan.pay.mapper.AccountPayRelationMapper;
import com.quanyan.pay.service.AccountPayRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Zheng.Ke
 * Date 2016/7/28.
 */
@Service
public class AccountPayRelationServiceImpl implements AccountPayRelationService {
    @Autowired
    private AccountPayRelationMapper accountPayRelationMapper;

    @Override
    public AccountPayRelation getAccountPayRelation(String mchId, Integer payId) {
        AccountPayRelationExample accountPayRelationExample = new AccountPayRelationExample();
        AccountPayRelationExample.Criteria accountCriteria = accountPayRelationExample.createCriteria();
        accountCriteria.andMchIdEqualTo(mchId);
        accountCriteria.andPayIdEqualTo(payId);

        int count = this.accountPayRelationMapper.countByExample(accountPayRelationExample);
        if (count == 0) {
            accountPayRelationExample.clear();
            accountCriteria = accountPayRelationExample.createCriteria();
            accountCriteria.andMchIdEqualTo("2"); //默认为2
            accountCriteria.andPayIdEqualTo(payId);
        }

        List<AccountPayRelation> accountPayRelations = this.accountPayRelationMapper.selectByExample(accountPayRelationExample);
        if (accountPayRelations.isEmpty()) {
            return null;
        }
        return accountPayRelations.get(0);
    }

    @Override
    public AccountPayRelation getAccountPayRelation(String mchId, Integer payId,Integer larkAppId) {
        AccountPayRelationExample accountPayRelationExample = new AccountPayRelationExample();
        AccountPayRelationExample.Criteria accountCriteria = accountPayRelationExample.createCriteria();
        accountCriteria.andMchIdEqualTo(mchId);
        accountCriteria.andPayIdEqualTo(payId);
        accountCriteria.andLarkAppIdEqualTo(larkAppId);
        int count = this.accountPayRelationMapper.countByExample(accountPayRelationExample);
        if (count == 0) {
            accountPayRelationExample.clear();
            accountCriteria = accountPayRelationExample.createCriteria();
            accountCriteria.andMchIdEqualTo("2"); //默认为2
            accountCriteria.andPayIdEqualTo(payId);
            accountCriteria.andLarkAppIdEqualTo(1);
        }

        List<AccountPayRelation> accountPayRelations = this.accountPayRelationMapper.selectByExample(accountPayRelationExample);
        if (accountPayRelations.isEmpty()) {
            return null;
        }
        return accountPayRelations.get(0);
    }
}
